/*
 * @Author: 肖思汗 
 * @Date: 2025-06-04 16:16:51 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-06-14 17:41:18
 */

import { isUndefined } from "lodash";
import { makeAutoObservable } from "mobx";
import goodsBaseApi from "src/api/goodsBaseApi";
import { GoodsBaseData } from "types/type";

class SizeColorPopupStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
    }
    // 颜色
    colors: GoodsBaseData["data"]["colors"] = [];
    selectedColor = "";
    selectedColorValue: `#${string}` = "#ffffff";
    // 尺码
    sizes: GoodsBaseData["data"]["sizes"] = [];
    selectedSize = "";
    // 显示弹出层
    open = false;

    productId?: number;
    // 底板id
    productTypeId: number = 0;

    designName?: string;

    setData({
        open,
        productId,
        productTypeId,
        selectedColor,
        selectedColorValue,
        designName
    }: {
        open: boolean,
        productId?: number,
        productTypeId?: number,
        selectedColor?: string,
        selectedColorValue?: `#${string}`,
        designName?: string,
    }) {
        this.open = open;
        this.productId = productId;
        if (!isUndefined(productTypeId)) {
            this.productTypeId = productTypeId;
            goodsBaseApi.getProductNegativeById(productTypeId).then((goodsBaseData) => {
                this.sizes = goodsBaseData.data.sizes.filter(s => s.enable);
                this.sizes[0] && (this.selectedSize = this.sizes[0].name);

                this.colors = goodsBaseData.data.colors;
                this.colors[0] && (this.selectedColor = selectedColor || this.colors[0].cn || "");
                this.colors[0] && (this.selectedColorValue = selectedColorValue || this.colors[0].color);

                this.designName = designName;
            });
        }

    }

}

const sizeColorPopupStore = new SizeColorPopupStore();

export default sizeColorPopupStore;